namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web.Mail;

    public class IssueNotification
    {
        private int _Id;
        private int _IssueId;
        private string _NotificationDisplayName;
        private string _NotificationEmail;
        private string _NotificationUsername;

        public IssueNotification(int issueId, string notificationUsername) : this(DefaultValues.GetIssueNotificationIdMinValue(), issueId, notificationUsername, string.Empty, string.Empty)
        {
        }

        public IssueNotification(int notificationId, int issueId, string notificationUsername, string notificationDisplayName, string notificationEmail)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("IssueId");
            }
            this._Id = notificationId;
            this._IssueId = issueId;
            this._NotificationUsername = notificationUsername;
            this._NotificationDisplayName = notificationDisplayName;
            this._NotificationEmail = notificationEmail;
        }

        public static bool DeleteIssueNotification(int issueId, string username)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteIssueNotification(issueId, username);
        }

        public static IssueNotificationCollection GetIssueNotificationsByIssueId(int issueId)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssueNotificationsByIssueId(issueId);
        }

        public bool Save()
        {
            int TempId = DataAccessLayerBaseClassHelper.GetDataAccessLayer().CreateNewIssueNotification(this);
            if (TempId > 0)
            {
                this._Id = TempId;
                return true;
            }
            return false;
        }

        public static void SendIssueNotifications(int issueId)
        {
            IEnumerator _Vb_t_ref_0 = null;
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            IssueNotificationCollection issNotifications = DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssueNotificationsByIssueId(issueId);
            SmtpMail.SmtpServer = Globals.SmtpServer;
            string notifyEmail = Globals.NotifyEmail;
            string desktopDefaultUrl = Globals.DesktopDefaultUrl;
            try
            {
                _Vb_t_ref_0 = issNotifications.GetEnumerator();
                while (_Vb_t_ref_0.MoveNext())
                {
                    IssueNotification notify = (IssueNotification) _Vb_t_ref_0.Current;
                    try
                    {
                        SmtpMail.Send(notifyEmail, notify.NotificationEmail, string.Format("Issue {0} has been updated", issueId), string.Format("The following issue has been updated: \n\n {0}Issues/IssueDetail.aspx?id={1}", desktopDefaultUrl, issueId));
                    }
                    catch (Exception exception1)
                    {
                        ProjectData.SetProjectError(exception1);
                        ProjectData.ClearProjectError();
                    }
                }
            }
            finally
            {
                if (_Vb_t_ref_0 is IDisposable)
                {
                    ((IDisposable) _Vb_t_ref_0).Dispose();
                }
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                if (value <= DefaultValues.GetIssueIdMinValue())
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                this._IssueId = value;
            }
        }

        public string NotificationDisplayName
        {
            get
            {
                if ((this._NotificationDisplayName == null) || (this._NotificationDisplayName.Length == 0))
                {
                    return string.Empty;
                }
                return this._NotificationDisplayName;
            }
        }

        public string NotificationEmail
        {
            get
            {
                if ((this._NotificationEmail == null) || (this._NotificationEmail.Length == 0))
                {
                    return string.Empty;
                }
                return this._NotificationEmail;
            }
        }

        public string NotificationUsername
        {
            get
            {
                if ((this._NotificationUsername == null) || (this._NotificationUsername.Length == 0))
                {
                    return string.Empty;
                }
                return this._NotificationUsername;
            }
        }
    }
}

